A unary function is a function that takes exactly one argument.
Here, square
is a unary function because it accepts only one parameter (x
).
Unary functions are common in functional programming, especially with methods like map
and filter
.
map
unary()
Function from LodashIn JavaScript, functions may receive more arguments than defined. To enforce unary behavior, you can use Function.prototype.length
or utilities like Lodash’s unary()
.
Without unary()
, parseInt
would receive unexpected arguments in map
, leading to incorrect results.
map()
.Would you like more examples or explanations?